"""
二分搜索算法
假定数组a是已按从小到大的顺序拍好
"""


def BinarySearch(a, x, n):
    l = 0
    r = n - 1
    while l <= r:
        mid = int((l + r) / 2)
        if a[mid] == x:
            return mid
        elif a[mid] > x:
            r = mid - 1
        elif a[mid] < x:
            l = mid + 1

    return -1


print("start")
a = [1, 2, 3, 6, 7, 8, 9, 30, 343, 45455, 543345, 453536767]
n = a.__len__()
print(n)
print(BinarySearch(a, 343, n))
